package com.huli.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huli.mapper.*;
import com.huli.pojo.*;
import com.huli.service.OrderInfoService;
import com.huli.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    // 注入OrderInfoMapper
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    // 注入UserMapper
    @Autowired
    private UserMapper sysUserMapper;

    // 注入SysDepartmentMapper
    @Autowired
    private SysDepartmentMapper sysDepartmentMapper;
    // 注入NursingProjectMapper
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private PastientMapper pastientMapper;

    @Autowired
    private SysOrganizationMapper sysOrganizationMapper;
    // 实现OrderInfoService接口中的orderInfoList方法
    @Override
    public OrganizationUserVo orderInfoList(OrganizationUserVo param) {
        // 使用PageHelper分页插件，设置当前页和每页显示的条数
        PageHelper.startPage(param.getPageNum(), param.getPageSize(),"create_time DESC");
        // 调用orderInfoMapper中的orderInfoList方法，获取订单信息列表
        List<OrderInfo> orderInfoList = orderInfoMapper.orderInfoList();
        // 遍历订单信息列表
        for (OrderInfo orderInfo : orderInfoList) {
            Patient patient = pastientMapper.findPastientById(orderInfo.getPatientId());
            orderInfo.setPatientName(patient.getPatientName());
            orderInfo.setPhone(patient.getPhone());
            orderInfo.setSex(patient.getSex());
            orderInfo.setAddress(patient.getIdNumber());
//            SysDepartment sysDepartment = sysDepartmentMapper.findDeptId(orderInfo.getDeptId());
//            // 将部门信息中的deptName设置到订单信息中
//            orderInfo.setDeptName(sysDepartment.getDeptName());
//            SysOrganization sysOrganization = sysOrganizationMapper.findOrganizationById(orderInfo.getOrgId());
//            orderInfo.setDeptName(sysOrganization.getOrgName());
            // 根据订单中的projectId，调用nursingProjectMapper中的findProjectId方法，获取护理项目信息
            NursingProject nursingProject = nursingProjectMapper.findProjectId(orderInfo.getProjectId());
            // 将护理项目信息中的projectName和price设置到订单信息中
            orderInfo.setProjectName(nursingProject.getProjectName());
            orderInfo.setProjectPrice(nursingProject.getPrice());
        }
        // 将订单信息列表封装到PageInfo中
        PageInfo<OrderInfo> orderInfoPageInfo = new PageInfo<>(orderInfoList);
        // 返回封装后的订单信息列表
        return new OrganizationUserVo(orderInfoPageInfo.getTotal(), orderInfoPageInfo.getList());
    }

    @Override
    public OrderInfo orderInfoById(Long id) {
        OrderInfo orderInfo = orderInfoMapper.orderInfoById(id);
        Patient patient = pastientMapper.findPastientById(orderInfo.getPatientId());
        orderInfo.setPatientName(patient.getPatientName());
        orderInfo.setPhone(patient.getPhone());
        orderInfo.setSex(patient.getSex());
        orderInfo.setAddress(patient.getIdNumber());
        SysDepartment sysDepartment = sysDepartmentMapper.findDeptId(orderInfo.getDeptId());
        // 将部门信息中的deptName设置到订单信息中
        orderInfo.setDeptName(sysDepartment.getDeptName());
        // 根据订单中的projectId，调用nursingProjectMapper中的findProjectId方法，获取护理项目信息
        NursingProject nursingProject = nursingProjectMapper.findProjectId(orderInfo.getProjectId());
        // 将护理项目信息中的projectName和price设置到订单信息中
        orderInfo.setProjectName(nursingProject.getProjectName());
        orderInfo.setProjectPrice(nursingProject.getPrice());
        return orderInfo;
    }

    @Override
    public PageInfo<OrderDetailVO> orderInfoList2(OrderDetailVO orderDetailVO) {
        PageHelper.startPage(orderDetailVO.getPageNum(), orderDetailVO.getPageSize());
        List<OrderDetailVO> orderInfoList = orderInfoMapper.orderInfoList2(orderDetailVO);
        PageInfo<OrderDetailVO> orderInfoPageInfo = new PageInfo<>(orderInfoList);
        return orderInfoPageInfo;
    }

    @Override
    public PageInfo<OrderInfoVo> orderInfoList55(OrderInfoVo orderInfoVo) {
        PageHelper.startPage(orderInfoVo.getPageNum(), orderInfoVo.getPageSize());
        List<OrderInfoVo> orderInfoList = orderInfoMapper.orderInfoList55(orderInfoVo);
        PageInfo<OrderInfoVo> orderInfoPageInfo = new PageInfo<>(orderInfoList);
        return orderInfoPageInfo;
    }

    @Override
    public PageInfo<OrderInfooVo> orderInfoList66(OrderInfooVo orderInfoVo) {
        PageHelper.startPage(orderInfoVo.getPageNum(), orderInfoVo.getPageSize());
        List<OrderInfooVo> orderInfoList = orderInfoMapper.orderInfoList66(orderInfoVo);
        PageInfo<OrderInfooVo> orderInfoPageInfo = new PageInfo<>(orderInfoList);
        return orderInfoPageInfo;
    }

        @Override
        public PageInfo<LogInfoVO> orderInfoList77(LogInfoVO logInfoVO) {
            PageHelper.startPage(logInfoVO.getPageNum(), logInfoVO.getPageSize(), "log_time DESC");
            List<LogInfoVO> list = orderInfoMapper.orderInfoList77(logInfoVO);
            PageInfo<LogInfoVO> orderInfoPageInfo = new PageInfo<>(list);
            return orderInfoPageInfo;
        }
    @Override
    public PageInfo<ServiceInfoVO> orderInfoList88(ServiceInfoVO serviceInfo) {
        PageHelper.startPage(serviceInfo.getPageNum(), serviceInfo.getPageSize());
        List<ServiceInfoVO> serviceInfoVOS = orderInfoMapper.orderInfoList88(serviceInfo);
        PageInfo<ServiceInfoVO> orderInfoPageInfo = new PageInfo<>(serviceInfoVOS);
        return orderInfoPageInfo;
    }

    @Override
    public List<OrderInfo> findAllOrderInfo() {
        List<OrderInfo> allOrderInfo = orderInfoMapper.findAllOrderInfo();
        return allOrderInfo;
    }


    //查询完护理费以及耗材费完成总费用

    @Override
    public void updateOrderInfo(OrderInfo orderInfo) {
        // 获取患者列表
        List<Patient> patientList = pastientMapper.patientList();
        // 获取护理项目列表
        List<NursingProject> nursingProjectList = nursingProjectMapper.nuringProjectListName();
        // 获取科室列表
        List<SysDepartment> sysDepartmentList = sysDepartmentMapper.departmentList();
        // 设置更新时间
        orderInfo.setUpdateTime(new Date());
        // 更新订单信息
        orderInfoMapper.updateOrderInfo(orderInfo);
    }

    @Override
    // 重写父类方法，根据传入的状态修改订单状态
    public void orderStatus(Integer status) {
        // 调用orderInfoMapper中的orderStatus方法，传入状态参数
        orderInfoMapper.orderStatus(status);
    }

    @Override
    // 重写父类方法
    public void orderInfoUser(Long id, Long userId) {
        // 调用orderInfoMapper中的orderInfoUser方法，传入id和userId参数
        orderInfoMapper.orderInfoUser(id,userId);
    }

    @Override
    public OrganizationUserVo orderInfoStatusList(OrganizationUserVo param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<OrderInfo> orderInfoList = orderInfoMapper.orderInfoStatusList();
        // 遍历订单信息列表
        for (OrderInfo orderInfo : orderInfoList) {
            Patient patient = pastientMapper.findPastientById(orderInfo.getPatientId());
            orderInfo.setPatientName(patient.getPatientName());
            orderInfo.setPhone(patient.getPhone());
            orderInfo.setSex(patient.getSex());
            orderInfo.setAddress(patient.getIdNumber());
//            SysDepartment sysDepartment = sysDepartmentMapper.findDeptId(orderInfo.getDeptId());
//            // 将部门信息中的deptName设置到订单信息中
//            orderInfo.setDeptName(sysDepartment.getDeptName());

            SysOrganization sysOrganization = sysOrganizationMapper.findOrganizationById(orderInfo.getOrgId());
            orderInfo.setDeptName(sysOrganization.getOrgName());
            // 根据订单中的projectId，调用nursingProjectMapper中的findProjectId方法，获取护理项目信息
            NursingProject nursingProject = nursingProjectMapper.findProjectId(orderInfo.getProjectId());
            // 将护理项目信息中的projectName和price设置到订单信息中
            orderInfo.setProjectName(nursingProject.getProjectName());
            orderInfo.setProjectPrice(nursingProject.getPrice());
        }
        // 将订单信息列表封装到PageInfo中
        PageInfo<OrderInfo> orderInfoPageInfo = new PageInfo<>(orderInfoList);
        // 返回封装后的订单信息列表
        return new OrganizationUserVo(orderInfoPageInfo.getTotal(), orderInfoPageInfo.getList());
    }

    @Override
    public OrganizationUserVo orderInfoStatusList0(OrganizationUserVo param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize(),"create_time DESC");
        List<OrderInfo> orderInfoList = orderInfoMapper.orderInfoStatusList0();
        // 遍历订单信息列表
        for (OrderInfo orderInfo : orderInfoList) {
            Patient patient = pastientMapper.findPastientById(orderInfo.getPatientId());
            orderInfo.setPatientName(patient.getPatientName());
            orderInfo.setPhone(patient.getPhone());
            orderInfo.setSex(patient.getSex());
            orderInfo.setAddress(patient.getIdNumber());
//            SysDepartment sysDepartment = sysDepartmentMapper.findDeptId(orderInfo.getDeptId());
//            // 将部门信息中的deptName设置到订单信息中
//            orderInfo.setDeptName(sysDepartment.getDeptName());
//            SysOrganization sysOrganization = sysOrganizationMapper.findOrganizationById(orderInfo.getOrgId());
//            orderInfo.setDeptName(sysOrganization.getOrgName());
            // 根据订单中的projectId，调用nursingProjectMapper中的findProjectId方法，获取护理项目信息
            NursingProject nursingProject = nursingProjectMapper.findProjectId(orderInfo.getProjectId());
            // 将护理项目信息中的projectName和price设置到订单信息中
            orderInfo.setProjectName(nursingProject.getProjectName());
            orderInfo.setProjectPrice(nursingProject.getPrice());
        }
        // 将订单信息列表封装到PageInfo中
        PageInfo<OrderInfo> orderInfoPageInfo = new PageInfo<>(orderInfoList);
        // 返回封装后的订单信息列表
        return new OrganizationUserVo(orderInfoPageInfo.getTotal(), orderInfoPageInfo.getList());
    }


}